package com.javaee.ch11.web;

import com.javaee.ch11.common.response.ResultVo;
import com.javaee.ch11.entity.Department;
import com.javaee.ch11.entity.Reader;
import com.javaee.ch11.service.DepartmentService;
import com.javaee.ch11.web.dto.DepartmentQueryDto;
import com.javaee.ch11.web.dto.ReaderQueryDto;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Validated
@RestController
@RequestMapping("/api/department")
public class DepartmentController {

    @Autowired
    DepartmentService departmentService;

    @PostMapping("/query")
    public ResultVo<List<Department>> query(@RequestBody DepartmentQueryDto queryDto) {
        return ResultVo.ok(departmentService.queryDepartment(queryDto));
    }

    /**
     * 获取指定ID的部门信息。
     *
     * @param id 部门ID
     * @return 包含部门对象的ResultVo
     */
    @GetMapping("/{id}")
    public ResultVo<Department> get(@PathVariable @NotNull(message = "部门ID不能为空") Long id) {
        Department department = departmentService.getById(id);
        return ResultVo.ok(department);
    }

    /**
     * 添加新部门。
     *
     * @param department 新部门对象
     * @return 操作结果的ResultVo
     */
    @PostMapping("/")
    public ResultVo add(@Valid @RequestBody Department department) {
        departmentService.add(department);
        return ResultVo.ok();
    }

    /**
     * 更新部门信息。
     *
     * @param department 要更新的部门对象
     * @return 操作结果的ResultVo
     */
    @PutMapping("/")
    public ResultVo update(@Valid @RequestBody Department department) {

        departmentService.updateById(department);
        return ResultVo.ok();
    }

    /**
     * 删除指定ID的部门。
     *
     * @param id 部门ID，不能为空
     * @return 操作结果的ResultVo
     */
    @DeleteMapping("/{id}")
    public ResultVo delete(@PathVariable @NotNull(message = "部门ID不能为空") Long id) {
        departmentService.deleteById(id);
        return ResultVo.ok();
    }

    /**
     * 获取所有部门列表。
     *
     * @return 包含部门列表的ResultVo
     */
    @GetMapping("/")
    public ResultVo list() {
        return ResultVo.ok(departmentService.listAll());
    }
}    